#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>

#include <iostream>
#include <cstdio>

#include "prot.h"
#include "paging_api.h"

#include "result_str.h"

using namespace std;


void printDiff (interrupt_info *r1, interrupt_info *r2, int n, char *fn1, char *fn2)
{
  int nOk = 0;
  int len = max (strlen(fn1), strlen(fn2));
  
  for (int i=0; i<n; ++i) {
    if (memcmp(r1, r2, sizeof(interrupt_info)) == 0) {
      printf("%d: OK, ", i);
      print_int_info(r1);
      ++nOk;
    }
    else {
      printf("%d: FAIL,\n", i);
      printf("   %*s: ", len, fn1);
      print_int_info(r1);
      printf("   %*s: ", len, fn2);
      print_int_info(r2);
    }
    ++r1;
    ++r2;
  }
  
  printf("\n=====================================\n");
  printf("%d OK from %d total.\n\n", nOk, n);
}



int main(int argc, char *argv[])
{
  struct stat s;
  int fd1 = open(argv[1], O_RDONLY);
  int fd2 = open(argv[2], O_RDONLY);
  fstat(fd1, &s);
  
  int size1 = s.st_size;
  fstat(fd2, &s);
  
  int size2 = s.st_size;
  int n1 = size1 / sizeof(interrupt_info);
  int n2 = size2 / sizeof(interrupt_info);
  
  if(n1 != n2) {
    cout << "'" << argv[1] << "' has " << n1 << " test case results, when '" << argv[2] << "' has " << n2 << "." << endl << "Only firts " << min(n1, n2) << "will be compared" << endl << endl;
  }
  
  interrupt_info *r1 = (interrupt_info *) new char[size1];
  interrupt_info *r2 = (interrupt_info *) new char[size2];
  read(fd1, r1, size1);
  read(fd2, r2, size2);
  close(fd1);
  close(fd2);
  
  printDiff(r1, r2, min(n1, n2), argv[1], argv[2]);
  
  delete r1;
  delete r2;
  
  return 0;
}
